$(document).ready(function(){
	//展示详细考题信息
	$(".show-test").click(function(){
		var testId = $(this).parent().siblings().eq(1).text();
		var path = $("#path").val();
		$.ajax({
			type: 'POST',
			url:path+'/getTestById',
			data:{
				testId : testId
			},
			dataType : "json",
			success:function(data){
				$('#showTestModal .sub-name').text(data.name);
				$('#showTestModal .content').text(data.content);
				$('#showTestModal .test-type').text(data.testType);
				$('#showTestModal .test-type-score').text(data.testTypeScore);
				$('#showTestModal .choose_a').text(data.chooseA);
				$('#showTestModal .choose_b').text(data.chooseB);
				$('#showTestModal .choose_c').text(data.chooseC);
				$('#showTestModal .choose_d').text(data.chooseD);
				$('#showTestModal .answer').text(data.answer);
			}
		});
		$('#showTestModal').modal('show');
	});
	//展示新增考题模态框并加载数据
	$(".add-test").click(function(){
		var path = $("#path").val();
		$.ajax({
			type : 'POST',
			url :path+'/getSubjectAndTestType',
			data : {},
			dataType : "json",
			success : function(data){
				$('#addTestForm select[name="sub_id"]').empty();
				$('#addTestForm select[name="testType"]').empty();
				$.each(data.subjectVOList ,function(index , item){
					$('#addTestForm select[name="sub_id"]').append('<option value = "'+item.sub_id+'">'+item.name+'</option>');
				});
				$.each(data.examTestTypeVOList,function(index , item){
					$('#addTestForm select[name="testType"]').append('<option value = "'+item.id+'" score ="'+item.testTypeScore+'">'+item.testType+'</option>');
				});
				$('#addTestForm select[name="testType"]').trigger('change');
			}
		});
		$('#addTestModal').modal('show');
	});
	//题目类型改变后
	$('#addTestForm select[name="testType"]').change(function(){
		$('#addTestForm .testTypeScore').text('   本题每题'+$(this).find('option:checked').attr('score')+'分');
		var testType = $(this).val();
		if(testType == 1){
			$('#addTestForm .answerTotal input:radio').show();
			$('#addTestForm .answerTotal input:checkbox').hide();
		}else if(testType == 2 || testType == 3){
			$('#addTestForm .answerTotal input:radio').hide();
			$('#addTestForm .answerTotal input:checkbox').show();
		}else{
			alert('无该试题类型');
		}
	});
	//保存新增的考题
	$("#addTestSaveBtn").click(function(){
		var path = $("#path").val();
		var sub_id = $('#addTestForm select[name="sub_id"]').val();
		var testType = $('#addTestForm select[name="testType"]').val();
		var content = $('#addTestForm textarea[name="content"]').val();
		var chooseA = $('#addTestForm input[name="chooseA"]').val();
		var chooseB = $('#addTestForm input[name="chooseB"]').val();
		var chooseC = $('#addTestForm input[name="chooseC"]').val();
		var chooseD = $('#addTestForm input[name="chooseD"]').val();
		var answerString = '';
		if(testType == 1){
			answerString +=$('#addTestForm input[name="answer"]:radio:checked').val();
		}else if(testType == 2 || testType == 3){
			var array = $('#addTestForm input[name="answer"]:checkbox:checked');
			$.each(array,function(index ,item){
				answerString += (index == 0 ? $(item).val() : (','+$(item).val()));
			});
				
			
		}else{
			alert("无此类型的题！");
		}
		$.ajax({
			type : 'POST',
			url : path+'/saveTest',
			data :{
				sub_id : sub_id,
				testType : testType,
				content : content,
				chooseA : chooseA,
				chooseB : chooseB,
				chooseC : chooseC,
				chooseD : chooseD,
				answerString : answerString				
			},
			dataType : "json",
			success : function(data){
				if(data.success){
					$('#addTestModal').modal('hide');
					alert("考题添加成功");
					window.location.reload();
					
				}else{
					$('#addTestModal').modal('hide');
					var messageMap = data.messageMap;
					alert("考题添加失败 --" + messageMap['message']);
				}
			}
		});
		
	});
	//弹出题目修改模态框并加载数据	
	$(".edit-test").click(function(e){	
		var testId = $(this).parent().siblings().eq(1).text();
		var path = $("#path").val();		
		$.ajax({
			type: 'POST',
			url:path+'/toEditTest',
			data:{
				testId : testId
			},
			dataType : "json",
			success:function(data){
				$('#editTestForm select[name="sub_id"]').empty();
				$('#editTestForm select[name="testType"]').empty();
				$.each(data.subjectVOList ,function(index , item){
					if(item.sub_id == data.testVO.sub_id){
						$('#editTestForm select[name="sub_id"]').append('<option value = "'+item.sub_id+'"  selected >'+item.name+'</option>');
					}else{
						$('#editTestForm select[name="sub_id"]').append('<option value = "'+item.sub_id+'">'+item.name+'</option>');
					}
					
				});
				$.each(data.examTestTypeVOList,function(index , item){
					if(item.id == data.testVO.testTypeId){
						$('#editTestForm select[name="testType"]').append('<option value = "'+item.id+'" score ="'+item.testTypeScore+'"  selected >'+item.testType+'</option>');
					}else{
						$('#editTestForm select[name="testType"]').append('<option value = "'+item.id+'" score ="'+item.testTypeScore+'">'+item.testType+'</option>');
					}					
				});
				$('#editTestForm #testId').val(data.testVO.id);
				$('#editTestForm textarea[name="content"]').text(data.testVO.content);
				$('#editTestForm input[name="chooseA"]').val(data.testVO.chooseA);
				$('#editTestForm input[name="chooseB"]').val(data.testVO.chooseB);
				$('#editTestForm input[name="chooseC"]').val(data.testVO.chooseC);
				$('#editTestForm input[name="chooseD"]').val(data.testVO.chooseD);
				$('#editTestForm select[name="testType"]').trigger('change');
				
				if(data.testVO.testTypeId == 1){
				$('#editTestForm input[name="answer"]:radio').each(function(){
					    if($.trim($(this).val()) == $.trim(data.testVO.answer)){
					       $(this).prop('checked',true);					       
					    }else{
					    	$(this).prop('checked',false);
					    }
					});			
				}else{
					    var answers = new Array();
					    var answerString = data.testVO.answer;
					    alert(answerString);
					    answers = answerString.split(",");
					    $('#editTestForm .answerTotal input[name="answer"]').each(function(){
					    	$(this).prop('checked',false);
					    });			    		
					    $.each(answers,function(index,item){
					    	
					    	alert(index+"=="+item)
					    	$('#editTestForm .answerTotal input[value="'+item+'"]:checkbox').prop('checked','checked');
					    });
			
				}
			   
				
			}
		});
		$('#editTestModal').modal('show');
		
	});
	//题目类型改变后
	$('#editTestForm select[name="testType"]').change(function(){
		$('#editTestForm .testTypeScore').text('   本题每题'+$(this).find('option:checked').attr('score')+'分');
		var testType = $(this).val();
		if(testType == 1){
			$('#editTestForm .answerTotal input:radio').show();		
			$('#editTestForm .answerTotal input:checkbox').hide();
		}else if(testType == 2 || testType == 3){
			$('#editTestForm .answerTotal input:radio').hide();
			$('#editTestForm .answerTotal input:checkbox').show();	
		}else{
			alert('无该试题类型');
		}
	});
	//保存修改后的数据
	$("#editTestBtn").click(function(){			
		var path = $("#path").val();
		var testId = $('#editTestForm #testId').val();	
		var sub_id = $('#editTestForm select[name="sub_id"]').val();
		var testType = $('#editTestForm select[name="testType"]').val();
		var content = $('#editTestForm textarea[name="content"]').val();
		var chooseA = $('#editTestForm input[name="chooseA"]').val();
		var chooseB = $('#editTestForm input[name="chooseB"]').val();
		var chooseC = $('#editTestForm input[name="chooseC"]').val();
		var chooseD = $('#editTestForm input[name="chooseD"]').val();
		var answerString = '';
		if(testType == 1){
			answerString +=$('#editTestForm input[name="answer"]:radio:checked').val();
		}else if(testType == 2 || testType == 3){
			var array = $('#editTestForm input[name="answer"]:checkbox:checked');
			$.each(array,function(index ,item){
				answerString += (index == 0 ? $(item).val() : (','+$(item).val()));
			});
				
			
		}else{
			alert("无此类型的题！");
		}
		$.ajax({
			type : 'POST',
			url : path+'/editTest',
			data :{
				
				testId : testId,
				sub_id : sub_id,
				testType : testType,
				content : content,
				chooseA : chooseA,
				chooseB : chooseB,
				chooseC : chooseC,
				chooseD : chooseD,
				answerString : answerString				
			},
			dataType : "json",
			success : function(data){
				if(data.success){
					$('#editTestModal').modal('hide');
					alert("考题修改成功");
					window.location.reload();
					
				}else{
					$('#editTestModal').modal('hide');
					var messageMap = data.messageMap;
					alert("考题修改失败 --" + messageMap['message']);
				}
			}
		});
	});
	//删除考题
	$(".delete-test").click(function(){
		if(confirm("确定删除该考题吗？（删除后将无法恢复哦）")){
			var path = $("#path").val();
			var testId = $(this).parent().siblings().eq(1).text();
			$.ajax({
				type : 'POST',
				url : path+'/deleteTest',
				data :{					
					testId : testId,								
				},
				dataType : "json",
				success : function(data){
					if(data.success){					
						alert("考题删除成功");
						window.location.reload();
						
					}else{						
						var messageMap = data.messageMap;
						alert("考题删除失败 --" + messageMap['message']);
					}
				}
			});
		}
	});
	
});